package org.scalaconsole.ui

import java.awt.Font

import java.awt.event._
import java.awt.event.InputEvent._
import java.awt.event.KeyEvent._

import javax.swing.JComponent._

import Preferences._

object Command {
  val list = List(Execute, Reset, IncreaseFont, DecreaseFont)
}

sealed abstract case class Command(val description: String, val when: Int) {
  lazy val key = Preferences.getKey(this)
  lazy val modifierMask: Int = Preferences.getModifierMask(this)

  def getKeyModifiersText() = InputEvent.getModifiersExText(modifierMask)
  def getKeyText() = KeyEvent.getKeyText(key)
}

object Execute extends Command("Execute", WHEN_IN_FOCUSED_WINDOW)
object Reset extends Command("Reset", WHEN_IN_FOCUSED_WINDOW)
object IncreaseFont extends Command("Increase font size", WHEN_IN_FOCUSED_WINDOW)
object DecreaseFont extends Command("Decrease font size", WHEN_IN_FOCUSED_WINDOW)

object Preferences {
  lazy val defaultFont = new Font("Courier", Font.PLAIN, 12)

  /**
   * The default modifier for a command.
   * (e.g. Ctrl on Windows or *nix systems, the Command key on Macs, etc.)
   */
  val defaultPrimaryModifier = System getProperty "os.name" match {
    case osName: String  if (osName != null && (osName contains "Mac")) => META_DOWN_MASK
    case _ => CTRL_DOWN_MASK
  }

  def getKey(command: Command) = command match {
    case Execute => VK_ENTER
    case Reset => VK_R
    case IncreaseFont => VK_EQUALS
    case DecreaseFont => VK_MINUS
  }

  def getModifierMask(command: Command) = command match {
    case _ => defaultPrimaryModifier
  }
}
